import json

from django.http import JsonResponse
from django.views import View

from app.models import *
from app.url import *
from app.respositories.respositories import consumer_coins


# 获取发送收到的礼物
class GiftViews(View):
    def get(self, request):
        try:
            # 获取用户id
            u_id = request.GET.get('u_id', '')
            # 获取所有送礼物记录并返回
            def gifts(name, url, video, _id):
                return {
                    'id': _id,
                    'count': GiftTransaction.objects.select_related('user').filter(user__id=u_id,
                                                                                   gift__name=name).count(),
                    'name': name,
                    'url': get_server_url(request) + url,
                    'video': get_server_url(request) + video
                }

            data = []
            gift_transaction = Gifts.objects.all()
            for gift in gift_transaction:
                data.append(gifts(gift.name, gift.image, gift.video, gift.id))

            def gifts(name, url):
                return {
                    'count': GiftTransaction.objects.select_related('user').filter(send_user__id=u_id,
                                                                                   gift__name=name).count(),
                    'name': name,
                    'url': get_server_url(request) + url
                }

            datas = []
            gift_transaction = Gifts.objects.all()
            for gift in gift_transaction:
                datas.append(gifts(gift.name, gift.image))
            return JsonResponse({
                'code': 200,
                'msg': '获取成功',
                'data': {
                    'result': {
                        'send': data,
                        'receive': datas
                    }
                }
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'data': {},
                'msg': f'获取失败：{str(e)}'
            })

    # 兑换金币
    def post(self, request):
        try:
            # 获取用户id
            u_id = json.loads(request.body).get('u_id', '')
            print(u_id, '222')
            count = User.objects.select_related('gold').get(gold_id=u_id)
            glod = 0
            gift_transaction = Gifts.objects.all()
            for gift in gift_transaction:
                glod += GiftTransaction.objects.select_related('user').filter(user__id=u_id,
                                                                              gift__name=gift.name).count() * gift.price
            GiftTransaction.objects.filter(user__id=u_id).delete()
            g = Gold.objects.get(id=u_id)
            g.money = (glod / 2) + count.gold.money
            g.save()
            consumer_coins({
                'user_id': u_id,
                'limit': float(glod / 2),
                'msg': '金币兑换'
            })
            return JsonResponse({
                'code': 200,
                'data': {
                    'gold': glod / 2
                },
                'msg': '兑换成功'
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'msg': str(e)
            })
